﻿using System;
using System.Collections.Generic;
using System.Text;
using PersistenceObject.Framework.Interface;
using System.Data;
using PersistenceObject.Framework.Expressions;

namespace PersistenceObject.Framework
{
    public class Pager : IPager
    {
        #region IPager 成员
        IQuery query;

        Int32 size;

        Int32 pageIndex;

        public IQuery Query
        {
            get { return this.query; }
            set { this.query = value; }
        }

        public int GetCount()
        {
            query.Selects = new FieldAdapter[] { new FieldAdapter("Count(*)") };

            object obj = query.ExecuteScalar();

            if (obj != null)
                return Convert.ToInt32(obj);

            return 0;
        }

        public int Size
        {
            get { return this.size; }
            set { this.size = value; }
        }

        public int PageIndex
        {
            get { return this.pageIndex; }
            set { this.pageIndex = value; }
        }

        public IList<T> List<T>() where T : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T>(query, size, pageIndex);
        }

        public IList<T> List<T>(Int32 pageIndex) where T : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T>(query, size, pageIndex);
        }

        public IList<Combined<T1, T2>> List<T1, T2>() 
            where T1 : class
            where T2 : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T1, T2>(query, size, pageIndex);
        }

        public IList<Combined<T1, T2>> List<T1, T2>(Int32 pageIndex)
            where T1 : class
            where T2 : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T1, T2>(query, size, pageIndex);
        }

        public IList<Combined<T1, T2, T3>> List<T1, T2, T3>()
            where T1 : class
            where T2 : class
            where T3 : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T1, T2, T3>(query, size, pageIndex);
        }

        public IList<Combined<T1, T2, T3>> List<T1, T2, T3>(Int32 pageIndex)
            where T1 : class
            where T2 : class
            where T3 : class
        {
            return Query.Session.DbDriver.CreatePageQuery<T1, T2, T3>(query, size, pageIndex);
        }

        #endregion
    }
}
